{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 14.9 \u6355\u83b7\u5f02\u5e38\u540e\u629b\u51fa\u53e6\u5916\u7684\u5f02\u5e38\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u6355\u83b7\u4e00\u4e2a\u5f02\u5e38\u540e\u629b\u51fa\u53e6\u5916\u4e00\u4e2a\u4e0d\u540c\u7684\u5f02\u5e38\uff0c\u540c\u65f6\u8fd8\u5f97\u5728\u5f02\u5e38\u56de\u6eaf\u4e2d\u4fdd\u7559\u4e24\u4e2a\u5f02\u5e38\u7684\u4fe1\u606f\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u94fe\u63a5\u5f02\u5e38\uff0c\u4f7f\u7528 raise from \u8bed\u53e5\u6765\u4ee3\u66ff\u7b80\u5355\u7684 raise \u8bed\u53e5\u3002\n\u5b83\u4f1a\u8ba9\u4f60\u540c\u65f6\u4fdd\u7559\u4e24\u4e2a\u5f02\u5e38\u7684\u4fe1\u606f\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def example():\n    try:\n            int('N/A')\n    except ValueError as e:\n            raise RuntimeError('A parsing error occurred') from e\nexample()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0a\u9762\u7684\u5f02\u5e38\u662f\u4e0b\u9762\u7684\u5f02\u5e38\u4ea7\u751f\u7684\u76f4\u63a5\u539f\u56e0\uff1a"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u56de\u6eaf\u4e2d\u53ef\u4ee5\u770b\u5230\uff0c\u4e24\u4e2a\u5f02\u5e38\u90fd\u88ab\u6355\u83b7\u3002\n\u8981\u60f3\u6355\u83b7\u8fd9\u6837\u7684\u5f02\u5e38\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u7b80\u5355\u7684 except \u8bed\u53e5\u3002\n\u4e0d\u8fc7\uff0c\u4f60\u8fd8\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b\u5f02\u5e38\u5bf9\u8c61\u7684 __cause__ \u5c5e\u6027\u6765\u8ddf\u8e2a\u5f02\u5e38\u94fe\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "try:\n    example()\nexcept RuntimeError as e:\n    print(\"It didn't work:\", e)\n\n    if e.__cause__:\n        print('Cause:', e.__cause__)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u5728 except \u5757\u4e2d\u53c8\u6709\u53e6\u5916\u7684\u5f02\u5e38\u88ab\u629b\u51fa\u65f6\u4f1a\u5bfc\u81f4\u4e00\u4e2a\u9690\u85cf\u7684\u5f02\u5e38\u94fe\u7684\u51fa\u73b0\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def example2():\n    try:\n            int('N/A')\n    except ValueError as e:\n            print(\"Couldn't parse:\", err)\nexample2()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u5904\u7406\u4e0a\u8ff0\u5f02\u5e38\u7684\u65f6\u5019\uff0c\u53e6\u5916\u4e00\u4e2a\u5f02\u5e38\u53d1\u751f\u4e86\uff1a"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f60\u540c\u65f6\u83b7\u5f97\u4e86\u4e24\u4e2a\u5f02\u5e38\u7684\u4fe1\u606f\uff0c\u4f46\u662f\u5bf9\u5f02\u5e38\u7684\u89e3\u91ca\u4e0d\u540c\u3002\n\u8fd9\u65f6\u5019\uff0cNameError \u5f02\u5e38\u88ab\u4f5c\u4e3a\u7a0b\u5e8f\u6700\u7ec8\u5f02\u5e38\u88ab\u629b\u51fa\uff0c\u800c\u4e0d\u662f\u4f4d\u4e8e\u89e3\u6790\u5f02\u5e38\u7684\u76f4\u63a5\u56de\u5e94\u4e2d\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\uff0c\u4f60\u60f3\u5ffd\u7565\u6389\u5f02\u5e38\u94fe\uff0c\u53ef\u4f7f\u7528 raise from None :"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def example3():\n    try:\n            int('N/A')\n    except ValueError:\n            raise RuntimeError('A parsing error occurred') from None"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8bbe\u8ba1\u4ee3\u7801\u65f6\uff0c\u5728\u53e6\u5916\u4e00\u4e2a except \u4ee3\u7801\u5757\u4e2d\u4f7f\u7528 raise \u8bed\u53e5\u7684\u65f6\u5019\u4f60\u8981\u7279\u522b\u5c0f\u5fc3\u4e86\u3002\n\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u79cd raise \u8bed\u53e5\u90fd\u5e94\u8be5\u88ab\u6539\u6210 raise from \u8bed\u53e5\u3002\u4e5f\u5c31\u662f\u8bf4\u4f60\u5e94\u8be5\u4f7f\u7528\u4e0b\u9762\u8fd9\u79cd\u5f62\u5f0f\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "try:\n   ...\nexcept SomeException as e:\n   raise DifferentException() from e"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u6837\u505a\u7684\u539f\u56e0\u662f\u4f60\u5e94\u8be5\u663e\u793a\u7684\u5c06\u539f\u56e0\u94fe\u63a5\u8d77\u6765\u3002\n\u4e5f\u5c31\u662f\u8bf4\uff0cDifferentException \u662f\u76f4\u63a5\u4ece SomeException \u884d\u751f\u800c\u6765\u3002\n\u8fd9\u79cd\u5173\u7cfb\u53ef\u4ee5\u4ece\u56de\u6eaf\u7ed3\u679c\u4e2d\u770b\u51fa\u6765\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u50cf\u4e0b\u9762\u8fd9\u6837\u5199\u4ee3\u7801\uff0c\u4f60\u4ecd\u7136\u4f1a\u5f97\u5230\u4e00\u4e2a\u94fe\u63a5\u5f02\u5e38\uff0c\n\u4e0d\u8fc7\u8fd9\u4e2a\u5e76\u6ca1\u6709\u5f88\u6e05\u6670\u7684\u8bf4\u660e\u8fd9\u4e2a\u5f02\u5e38\u94fe\u5230\u5e95\u662f\u5185\u90e8\u5f02\u5e38\u8fd8\u662f\u67d0\u4e2a\u672a\u77e5\u7684\u7f16\u7a0b\u9519\u8bef\u3002"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "try:\n   ...\nexcept SomeException:\n   raise DifferentException()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u4f60\u4f7f\u7528 raise from \u8bed\u53e5\u7684\u8bdd\uff0c\u5c31\u5f88\u6e05\u695a\u7684\u8868\u660e\u629b\u51fa\u7684\u662f\u7b2c\u4e8c\u4e2a\u5f02\u5e38\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\u4e00\u4e2a\u4f8b\u5b50\u4e2d\u9690\u85cf\u5f02\u5e38\u94fe\u4fe1\u606f\u3002\n\u5c3d\u7ba1\u9690\u85cf\u5f02\u5e38\u94fe\u4fe1\u606f\u4e0d\u5229\u4e8e\u56de\u6eaf\uff0c\u540c\u65f6\u5b83\u4e5f\u4e22\u5931\u4e86\u5f88\u591a\u6709\u7528\u7684\u8c03\u8bd5\u4fe1\u606f\u3002\n\u4e0d\u8fc7\u4e07\u4e8b\u7686\u5e73\u7b49\uff0c\u6709\u65f6\u5019\u53ea\u4fdd\u7559\u9002\u5f53\u7684\u4fe1\u606f\u4e5f\u662f\u5f88\u6709\u7528\u7684\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}